package mappers;

import java.util.ArrayList;
import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

import model.Question;
import model.Quizze;
import dto.QuestionDTO;
import dto.QuizzeDTO;

public class QuizzeMapper {

	@PersistenceContext
	EntityManager em;

	public QuizzeDTO mapDOMtoDTO(Quizze dom) {
		QuizzeDTO dto = new QuizzeDTO();

		dto.setId(dom.getId());
		dto.setName(dom.getName());
		List<QuestionDTO> questions = null;
		questions = new ArrayList<QuestionDTO>();
		QuestionMapper questionMapper = new QuestionMapper();
		for (Question question : dom.getQuestions()) {
			questions.add(questionMapper.mapDOMtoDTO(question));
		}
		dto.setQuestions(questions);

		return dto;
	}

	public Quizze mapDTOtoDOM(QuizzeDTO dto) {
		Quizze dom = null;

		if (dto.getId() == 0) {
			dom = new Quizze();
		} else {
			dom = em.find(Quizze.class, dto.getId());
		}
		dom.setName(dto.getName());
		List<Question> questions = null;
		questions = new ArrayList<Question>();
		QuestionMapper questionMapper = new QuestionMapper();
		for (QuestionDTO question : dto.getQuestions()) {
			questions.add(questionMapper.mapDTOtoDOM(question));
		}
		dom.setQuestions(questions);

		return dom;
	}

}
